<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="产品:" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="周期:" prop="period">
        <el-input
          v-model="queryParams.period"
          placeholder="请输入天数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wallet:walletMining:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    
    <el-table v-loading="loading" :data="walletMarketList" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="50" align="center" label="序号"/>
      
      <el-table-column label="产品名称" align="center" prop="name"/>
      <el-table-column label="单笔限额" align="center" prop="limitLow">
        <template #default="scope">
          <span>{{ scope.row.limitLow }}-{{scope.row.limitHigh}}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计收益" align="center" prop="expectedRevenue"/>
      <el-table-column label="实际收益" align="center" prop="realReturn"/>
      <el-table-column label="日收益" align="center" prop="dailyEarnings"/>
      <el-table-column label="周期（天）" align="center" prop="period"/>
      <el-table-column label="派息时间" align="center" prop="dividendPaymentTime"/>
      <el-table-column label="违约比例（%）" align="center" prop="earlyRedemption"/>
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="danger" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:mining:remove']">删除
          </el-button>
          <el-button link type="primary" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:mining:edit']">修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />
    
    <!-- 添加或修改矿机列表对话框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body>
      <el-form ref="miningRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称：" prop="name">
              <el-input v-model="form.name" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="预计收益：" prop="expectedRevenue">
              <el-input v-model="form.expectedRevenue"/>
            </el-form-item>
            <el-form-item label="日收益：" prop="dailyEarnings">
              <el-input v-model="form.dailyEarnings"/>
            </el-form-item>
            <el-form-item label="派息时间" prop="dividendPaymentTime">
              <el-select v-model="form.dividendPaymentTime">
                <el-option
                  label="每天"
                  value="每天"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单笔限额：">
              <el-col :span="10">
                <el-form-item prop="limitLow">
                  <el-input v-model="form.limitLow" placeholder="请输入"/>
                </el-form-item>
              </el-col>
              <el-col class="text-center" :span="2" style="margin: 0 0.5rem">至</el-col>
              <el-col :span="10">
                <el-form-item prop="limitHigh">
                  <el-input v-model="form.limitHigh" placeholder="请输入"/>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item label="实际收益：" prop="realReturn">
              <el-input v-model="form.realReturn"/>
            </el-form-item>
            <el-form-item label="周期（天）：" prop="period">
              <el-select v-model="form.period">
                <el-option
                  label="7"
                  value="7"
                />
                <el-option
                  label="14"
                  value="14"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="违约比例(%)：" prop="earlyRedemption">
              <el-input v-model="form.earlyRedemption" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WalletMining">
  import {addMining, delMining, getMining, listWalletMining, updateMining} from "@/api/boot/walletMining";
  import {parseTime} from "@/utils/ruoyi.js";
  
  const {proxy} = getCurrentInstance();
  
  const walletMarketList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  
  const data = reactive({
    form: {
    },
    queryParams: {
      current: 1,
      size: 10,
      type: null,
      status: null,
      positionStatus: null,
      startTime: null,
      endTime: null,
    },
    rules: {
      name: [
        { required: true, message: '产品名称不能为空', trigger: 'blur' },
      ],
      expectedRevenue: [
        { required: true, message: '预计收益不能为空', trigger: 'blur' },
      ],
      dailyEarnings: [
        { required: true, message: '日收益不能为空', trigger: 'blur' },
      ],
      dividendPaymentTime: [
        { required: true, message: '派息时间不能为空', trigger: 'blur' },
      ],
      limitLow: [
        { required: true, message: '单笔限额不能为空', trigger: 'blur' },
      ],
      limitHigh: [
        { required: true, message: '单笔限额不能为空', trigger: 'blur' },
      ],
      realReturn: [
        { required: true, message: '实际收益不能为空', trigger: 'blur' },
      ],
      period: [
        { required: true, message: '周期不能为空', trigger: 'blur' },
      ],
      earlyRedemption: [
        { required: true, message: '违约比例额不能为空', trigger: 'blur' },
      ],
    }
  });
  
  const {queryParams, form, rules} = toRefs(data);
  
  /** 查询市场买卖交易记录列表 */
  function getList() {
    loading.value = true;
    listWalletMining(queryParams.value).then(response => {
      walletMarketList.value = response.data.records;
      total.value = response.data.total;
      loading.value = false;
    });
  }
  
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }
  
  // 表单重置
  function reset() {
    form.value = {
      id: null,
      name: null,
      limitLow: 100,
      limitHigh: 1000,
      expectedRevenue: '70-140',
      dailyEarnings: '1-2%',
      realReturn: 1,
      period: 7,
      earlyRedemption: 0.01,
      dividendPaymentTime: '每天',
      createTime: null,
      endTime: null,
      updateTime: null
    };
    proxy.resetForm("miningRef");
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.current = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "新增矿机";
  }
  
  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getMining(_id).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改矿机";
    });
  }
  
  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["miningRef"].validate(valid => {
      if (valid) {
        if (form.value.id != null) {
          updateMining(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addMining(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }
  
  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除"' + row.name + '"的矿机？').then(function () {
      return delMining(_ids);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {
    });
  }
  
  getList();
</script>
